package cn.iocoder.yudao.module.bill.dal.mysql.bill_cate;

import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.bill.dal.dataobject.bill_cate.BillCateDO;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.bill.controller.admin.bill_cate.vo.*;
import org.apache.ibatis.annotations.Select;

/**
 * 账单分类 Mapper
 *
 * @author mmciel
 */
@Mapper
public interface BillCateMapper extends BaseMapperX<BillCateDO> {

    default List<BillCateDO> selectList(BillCateListReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<BillCateDO>()
                .likeIfPresent(BillCateDO::getClsName, reqVO.getClsName())
                .eqIfPresent(BillCateDO::getAmountType, reqVO.getAmountType())
                .eqIfPresent(BillCateDO::getParentId, reqVO.getParentId())
                .eqIfPresent(BillCateDO::getOrderNum, reqVO.getOrderNum())
                .likeIfPresent(BillCateDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(BillCateDO::getCreateTime, reqVO.getCreateTime())
                .orderByAsc(BillCateDO::getOrderNum)
                .orderByDesc(BillCateDO::getCreateTime));
    }

	default BillCateDO selectByParentIdAndClsName(Long parentId, String clsName) {
	    return selectOne(BillCateDO::getParentId, parentId, BillCateDO::getClsName, clsName);
	}

    default Long selectCountByParentId(Long parentId) {
        return selectCount(BillCateDO::getParentId, parentId);
    }


    @Select("""
        select a.amount_type,a.cls_name,b.cls_name as sub_cls_name
        from tb_bill_cate a
        left join tb_bill_cate b
        on a.id = b.parent_id
        where a.deleted = 0
        and a.parent_id = 0
        and b.deleted = 0
        order by a.order_num,b.order_num
    """)
    List<BillCateTreeRespVO> getCateTree();
}